package com.gf.exam.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gf.base.controller.BasicController;
import com.gf.base.type.EventType;
import com.gf.base.type.OperationType;
import com.gf.base.type.Role;
import com.gf.common.bean.DataRequest;
import com.gf.common.bean.DataResponse;
import com.gf.common.bean.Page;
import com.gf.common.constant.Constants;
import com.gf.common.constant.ViewPaths;
import com.gf.common.exception.NotOperationEexception;
import com.gf.common.util.WebHelper;
import com.gf.event.service.EventService;
import com.gf.exam.model.Answer;
import com.gf.exam.model.AnswerCell;
import com.gf.exam.model.Exam;
import com.gf.exam.service.AnswerService;
import com.gf.exam.service.ExamCellService;
import com.gf.user.model.User;

/**
 * 专门管理项目答案模块
 * 
 * @author zhuguifeng guifeng.net@gmail.com
 * @version 创建时间：2012-12-8 上午11:00:02
 */
@Controller
@RequestMapping("/answer")
public class AnswerController extends BasicController {

	@Autowired
	private AnswerService answerService;

	@Autowired
	private ExamCellService examCellService;

	@Autowired
	private EventService eventService;

	private static final Log logger = LogFactory.getLog(AnswerController.class);

	/**
	 * 查看答案首页
	 * 
	 * @param examId
	 * @return
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index(long id) {
		User user = WebHelper.getSessionUser();
		Exam exam = examCellService.scan(id, user.getId());
		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.addObject("exam", exam);
		mvc.setViewName(ViewPaths.ANSWER_INDEX);

		return mvc;
	}

	/**
	 * 用戶提交答卷
	 * 
	 * @param examId
	 * @param cellNo
	 * @param answerRight记录该张PPT用户回答是否正确
	 *            -> 1正确 | 0错误| -1本张没有标准答案
	 * @param mouseAction
	 * @param inputAction用户键盘输入记录
	 *            "d-f-s","d-r-d"
	 * @param rt
	 *            反应时间:ms array
	 *            "13-23-4324-343","23-43-24",1st就只有一个数,0的话表示刚页不用记录用户的反应时间
	 * @return
	 */
	@RequestMapping(value = "/submit", method = RequestMethod.POST)
	public @ResponseBody
	long submit(
			long examId,
			int cellNo,
			String title,
			@RequestParam(value = "answerRight[]", required = true) String[] answerRight,
			@RequestParam(value = "mouseAction[]", required = true) String[] mouseAction,
			@RequestParam(value = "inputAction[]", required = true) String[] inputAction,
			@RequestParam(value = "rt[]", required = true) String[] rt) {

		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return -1;
		}

		logger.info(user.getNickname() + " 提交答案");

		eventService.addEvent(user.getId(), user.getNickname(),
				OperationType.CAN_JIA, title, examId, EventType.PROJECT);

		return answerService.save(user.getId(), user.getNickname(), examId,
				cellNo, answerRight, mouseAction, inputAction, rt);
	}

	/**
	 * 该项目所有答案列表数据<br>
	 * 只能查看自己发布项目的答案
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse getAllData(DataRequest dataRequest, long examId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null || user.getRole().equals(Role.NORMAL)) {
			return new DataResponse();
		}

		Exam exam = examCellService.scan(examId, user.getId());
		if (exam == null) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = answerService.getAllAnswer(examId, curpage, pageSize);

		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}

	/**
	 * 查看某一用户的答卷 页面
	 * 
	 * @param id
	 * @param examId
	 * @return
	 */
	@RequestMapping(value = "/scanOneUser", method = RequestMethod.GET)
	public ModelAndView scanOneUser(long answerId, long examId) {
		User user = WebHelper.getSessionUser();

		// 查看当前用户是否有权限
		Exam exam = examCellService.scan(examId, user.getId());
		if (exam == null) {
			throw new NotOperationEexception(user.getNickname()
					+ " 您没有查看此答卷的权利");
		}

		Answer answer = answerService.scanById(answerId);

		ModelAndView mvc = new ModelAndView();

		mvc.setViewName(ViewPaths.ANSWER_SCAN_ONE_USER);
		mvc.addObject("exam", exam);
		mvc.addObject("answer", answer);

		return mvc;
	}

	/**
	 * 查看某用户的答案
	 * 
	 * @param answerId
	 * @param examId
	 * @return
	 */
	@RequestMapping(value = "/getAllAnswerCells", method = RequestMethod.POST)
	public @ResponseBody
	List<AnswerCell> getAllAnswerCells(long answerId, long examId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new ArrayList<AnswerCell>();
		}

		// 查看当前用户是否有权限
		Exam exam = examCellService.scan(examId, user.getId());
		if (exam == null) {
			return new ArrayList<AnswerCell>();
		}

		return answerService.getAnswerCells(answerId);
	}

	/**
	 * 得到该项目下所有的答案
	 * 
	 * @param examId
	 * @return Long answerId<br>
	 *         List 对应的下的所有answercell
	 */
	@RequestMapping(value = "/getAllAnswerCells2", method = RequestMethod.POST)
	public @ResponseBody
	Map<Long, List<AnswerCell>> getAllAnswerCells(long examId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new HashMap<Long, List<AnswerCell>>();
		}

		// 查看当前用户是否有权限
		Exam exam = examCellService.scan(examId, user.getId());
		if (exam == null) {
			return new HashMap<Long, List<AnswerCell>>();
		}

		return answerService.getAllAnsewerByExamId(examId);
	}

}
